import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { RuleForm, RulePageVO, RuleQuery, AddRuleForm } from "./types";

/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function getRulePage(
  queryParams: RuleQuery,
  courseId: number
): AxiosPromise<PageResult<RulePageVO[]>> {
  return request({
    url: "/api/v1/course/rule/page",
    method: "get",
    params: {
      ...queryParams,
      courseId,
    },
  });
}

/**
 * 获取用户分页列表
 *
 * @param queryParams
 */
export function ruleListInfo(ruleId: number): AxiosPromise<RuleForm[]> {
  return request({
    url: "/api/v1/course/rule/view",
    method: "get",
    params: {
      ruleId,
    },
  });
}

/**
 * 获取规则下拉树形列表
 */
export function listRuleOptions(id: number): AxiosPromise<OptionType[]> {
  return request({
    url: "/api/v1/course/rule/" + id + "/options",
    method: "get",
  });
}

/**
 * 获取规则表单数据
 *
 * @param id
 */
export function getRuleForm(id: number): AxiosPromise<RuleForm> {
  return request({
    url: "/api/v1/course/rule/" + id + "/form",
    method: "get",
  });
}

/**
 * 添加规则
 *
 * @param data
 */
export function addRule(data: RuleForm) {
  return request({
    url: "/api/v1/course/rule",
    method: "post",
    data: data,
  });
}

/**
 * 添加自定义规则
 *
 * @param data
 */
export function addCustomRule(data: AddRuleForm, courseId: number) {
  return request({
    url: "/api/v1/course/rule/custom",
    method: "post",
    data: data,
    params: {
      courseId,
    },
  });
}

/**
 * 修改规则
 *
 * @param id
 * @param data
 */
export function updateRule(id: number, data: RuleForm) {
  return request({
    url: "/api/v1/course/rule/" + id,
    method: "put",
    data: data,
  });
}

/**
 * 删除规则
 *
 * @param id 规则ID
 */
export function deleteRule(id: number) {
  return request({
    url: "/api/v1/course/rule/" + id,
    method: "delete",
  });
}

/**
 * 获取标准模板
 *
 */
export function getTemplateRules(): AxiosPromise<RulePageVO[]> {
  return request({
    url: "/api/v1/course/rule/template",
    method: "get",
  });
}

/**
 * 使用标准模板
 *
 */
export function saveRuleByTemplate(id: number, courseId: number) {
  return request({
    url: "/api/v1/course/rule/template/" + id + "/add",
    method: "post",
    params: {
      courseId,
    },
  });
}

/**
 * 删除规则
 *
 * @param ids
 */
export function deleteRules(ids: string) {
  return request({
    url: "/api/v1/course/rule/rootDelete/" + ids,
    method: "delete",
  });
}

/**
 * 获得课程列表选项
 *
 */
export function getCourseOptions(courseId: number): AxiosPromise<OptionType[]> {
  return request({
    url: "/api/v1/course/rule/courseOptions",
    method: "get",
    params: {
      courseId,
    },
  });
}

/**
 * 根据课程id获取课程创建的规则
 *
 */
export function getRuleByCourseId(
  courseId: number
): AxiosPromise<RulePageVO[]> {
  return request({
    url: "/api/v1/course/rule/getByCourseId",
    method: "get",
    params: {
      courseId,
    },
  });
}
